package speakhunter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.blobstore.BlobKey;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

public class FacebookPerfil extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	

		String nome = req.getParameter("name");
		String email = req.getParameter("email");
		String educacao = req.getParameter("education");
		String cidade = req.getParameter("hometown");
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Professor.class);
		query.setFilter("email == email_verificacao");
	    query.declareParameters("String email_verificacao");
		List<Professor> professor = (List<Professor>) pm.newQuery(query).execute(email);
		BlobKey leandro = null;
		if (professor.isEmpty()) {
			Professor professor_novo = new Professor(nome, "nenhum", "0,00", "nenhum", false, 0, educacao, email, null, cidade, null, leandro);
			try {
				pm.makePersistent(professor_novo);
			} finally {
				pm.close();
			}
		} else {
			try {
				for (Professor p : professor) {
					  p.setNome(nome);
					  p.setEmail(email);
					  p.setEducacao(educacao);
					  p.setCidade(cidade);
				};
		    } finally {
		        pm.close();
		    }
		}
		
		
    }
}